/**
 * 
 */
package com.ampaiva.organizescreen.activity;

import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

import com.ampaiva.organizescreen.R;
import com.ampaiva.organizescreen.controller.PuzzleController;
import com.ampaiva.organizescreen.model.Puzzle;

/**
 * @author ampaiva This is the main surface that handles the ontouch events and
 *         draws the image to the screen.
 */
public class PuzzlePanel extends SurfaceView implements SurfaceHolder.Callback {
	private final PuzzleController controller = new PuzzleController();
	private PuzzleDrawer puzzleDrawer;

	public PuzzlePanel(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		// adding the callback (this) to the surface holder to intercept events
		getHolder().addCallback(this);

		// make the GamePanel focusable so it can handle events
		setFocusable(true);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		controller.onSurfaceCreated(this);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		controller.onSurfaceChanged(holder, format, width, height);
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		controller.onSurfaceDestroyed();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN)
			controller.onActionDown((int) event.getX(), (int) event.getY());
		else if (event.getAction() == MotionEvent.ACTION_MOVE)
			controller.onActionMove((int) event.getX(), (int) event.getY());
		else if (event.getAction() == MotionEvent.ACTION_UP)
			controller.onActionUp((int) event.getX(), (int) event.getY());

		return true;
	}

	public void render(Canvas canvas) {
		getPuzzleDrawer().draw(canvas);
	}

	public void setPuzzle(Puzzle puzzle) {
		setPuzzleDrawer(new PuzzleDrawer(puzzle));
	}

	public void setFinished() {
		((Activity) getContext()).finish();
	}

	public void showEnd(long startTime) {
		AlertDialog ad = new AlertDialog.Builder(getContext()).create();
		ad.setCancelable(false); // This blocks the 'BACK' button
		ad.setMessage("You won! Your time is " + DateFormat.format("mm:ss", new Date(new Date().getTime() - startTime)));
		ad.setButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		ad.show();
	}

	public PuzzleDrawer getPuzzleDrawer() {
		return puzzleDrawer;
	}

	public void setPuzzleDrawer(PuzzleDrawer puzzleDrawer) {
		this.puzzleDrawer = puzzleDrawer;
	}

	public void setCount(int count) {
		TextView txtCount = (TextView) ((Activity) getContext()).findViewById(R.id.txtCount);
		if (txtCount != null)
			txtCount.setText(String.valueOf(count));
	}

}
